package com.example.administrator.myapplication.form;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.data.Task;
import com.example.administrator.myapplication.utils.DateUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/11/27.
 */

public class FormRecyclerAdapter extends RecyclerView.Adapter<FormRecyclerAdapter.ViewHolder>{

    private List<Task> mTasks;

    public FormRecyclerAdapter(List<Task> tasks) {
        this.mTasks = tasks;
    }

    public void replaceData(List<Task> tasks){
        this.mTasks = tasks;
        notifyDataSetChanged();
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_form_recycler,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.name.setText(mTasks.get(position).getName());
        holder.position.setText(mTasks.get(position).getPosition());
        holder.date.setText(DateUtil.formatDate(mTasks.get(position).getDate()));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(mTasks.get(position));
            }
        });
    }

    @Override
    public int getItemCount() {
        return mTasks.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{

        TextView name,position,date;
        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            position = itemView.findViewById(R.id.position);
            date = itemView.findViewById(R.id.date);
        }
    }

    private OnItemClickListener listener;

    public interface OnItemClickListener{
        void onClick(Task task);
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }
}
